package cn.zzm.mybatisplus.service.impl;

import cn.zzm.mybatisplus.base.IBaseService;
import cn.zzm.mybatisplus.dto.User;
import cn.zzm.mybatisplus.dto.UserDto;
import cn.zzm.mybatisplus.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.entity.vo.JsonResult;
import com.rt.util.JsonResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO().
 *
 * <p>TODO().
 *
 * @author 周昭名
 * @version V1.0
 * @since 2019/6/10 1:25
 */
@Service
public class UserServiceImpl implements IBaseService<UserDto,User> {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(String key) {
        int i = userMapper.deleteById(key);
        return i;
    }

    @Override
    public int insertSelective(User user) {
        int i = userMapper.insert(user);
        return i;
    }

    @Override
    public User selectByPrimaryKey(String key) {
        User user = userMapper.selectById(key);
        return user;
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        int i = userMapper.updateById(user);
        return i;
    }

    /**
      *
      *TODO(分页查询).
      *
      *<p>TODO().
      *
      *@return
      *@since 2019/6/12 11:26
    */
    public JsonResult getListPage(UserDto dto) {
        //构造查询条件
        QueryWrapper qw = getBaseQueryWrapper(dto);
        //分页查询
        IPage<User> page = new Page<User>(dto.getPageNo(), dto.getPageSize());
        page = userMapper.selectPage(page,qw);
        //构造分页查询
        JsonResult jsonResult = JsonResultUtil.success("操作成功", page);
        return jsonResult;
    }

    /**
      *
      *TODO(条件查询列表).
      *
      *<p>TODO().
      *
      *@return
      *@since 2019/6/12 11:26
    */
    @Override
    public List<User> getList(UserDto dto) {


        QueryWrapper qw = getBaseQueryWrapper(dto);
        List<User> list = userMapper.selectList(qw);

        return list;
    }
    /**
      *
      *TODO(查询全部结果).
      *
      *<p>TODO().
      *
      *@return
      *@since 2019/6/12 13:50
    */
    @Override
    public List<User> getListAll(UserDto dto) {
        List<User> list = userMapper.selectList(null);
        return list;
    }

    /**
      *
      *TODO(构造基本查询条件).
      *
      *<p>TODO().
      *
      *@return
      *@since 2019/6/12 11:26
    */
    private QueryWrapper getBaseQueryWrapper(UserDto dto) {
        //构造wrapper查询
        QueryWrapper qw = new QueryWrapper();
        //构造=查询条件
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", dto.getId());
        map.put("email", dto.getEmail());
        map.put("create_time", dto.getCreateTime());
        qw.allEq(map, false);
        //构造其他条件
        if (StringUtils.isNotBlank(dto.getName())) {
            qw.likeRight("name", dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getEndCreateTime())) {
            qw.ge("endCreateTime", dto.getEndCreateTime());
        }
        if (StringUtils.isNotBlank(dto.getEndCreateTime())) {
            qw.le("create_time", dto.getEndCreateTime());
        }
        return qw;
    }
}
